package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.BorrowAdapter
import com.ht.medicaltreatment.bean.BorrowPage
import com.ht.medicaltreatment.utils.DateUtils
import com.ht.medicaltreatment.view.ProjectDialog
import com.ht.medicaltreatment.view.TaskDeleteDialog
import com.ht.medicaltreatment.viewmodel.BorrowViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_borrow_list_layout.*

//借出订单列表
class BorrowListActivity:BaseActivity(), OnItemChildClickListener, View.OnClickListener,
    OnRefreshLoadMoreListener {

    override fun isViewStup() = true

    override fun getLayoutId() = R.layout.activity_borrow_list_layout

    private val adapter = BorrowAdapter()
    private val model by viewModels<BorrowViewModel>()
    private var page = 1
    private var sign = -1 //检索条件 0 未核验  1已核验
    private var projectId = 0
    private var isFirst = true

    override fun init() {
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemChildClickListener(this)

        imgRight.setOnClickListener {
            //创建计划表单
            startActivity(BuildBorrowOrderActivity::class.java)
        }

        imgBack.setOnClickListener {
            finish()
        }
        tvStartTime.setOnClickListener(this)
        tvEndTime.setOnClickListener(this)
        tvProjectName.setOnClickListener(this)
        cbAll.setOnClickListener(this)
        cbNormal.setOnClickListener(this)
        cbOverTime.setOnClickListener(this)
        btnPositive.setOnClickListener(this)
        etProjectName.setOnClickListener(this)

        refreshLayout.setOnRefreshLoadMoreListener(this)
        initData()
    }

    override fun onResume() {
        super.onResume()
        page = 1
        if(!isFirst){
            initData()
        }
    }

    private fun initData() {
        showProgress()
        var orderName = ""
        var sampelName = ""
        var sartTime = ""
        var endTime = ""
        isFirst = false
        if(etFormName != null){
            orderName = etFormName.text.toString()
            sampelName = etSampleName.text.toString()
            sartTime = tvStartTime.text.toString()
            endTime = tvEndTime.text.toString()
        }
        model.request(page,sign, orderName,sampelName
            ,"$projectId",sartTime, endTime
        ) {
            dismissProgress()
            if (it != null && page > 1) {
                adapter.addData(it)
                refreshLayout.finishLoadMore()
            } else {
                adapter.setNewInstance(it)
            }
            if (tvCheckCount != null) {
                tvCheckCount.text = "${adapter.data.size}"
            }
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as BorrowPage
        item.status
        if(view.id == R.id.tvDelete){
            //删除
            TaskDeleteDialog(this).showDialog {
                showProgress()
                val list = arrayListOf<String>()
                list.add("${item.id}")
                model.delete(list) {
                    dismissProgress()
                    showToast("删除成功")
                    adapter.removeAt(position)
                    tvCheckCount.text = "${adapter.data.size}"
                }
            }
        }else if(view.id == R.id.tvChange){
            //修改
            //val intent = Intent(this,ChangeInventoryOrderActivity::class.java)
            val intent = Intent(this,BorrowUpdateActivity::class.java)
            intent.putExtra("orderId",item.id)
            startActivity(intent)
        }else{
            //详情
            val intent = Intent(this,BorrowOrderDetailActivity::class.java)
            intent.putExtra("id",item.id)
            intent.putExtra("isChecked",item.status)
            startActivity(intent)
        }
    }

    override fun onClick(v: View?) {
        when(v){
            tvStartTime -> DateUtils.selectTime(this,"选择开始时间"){
                tvStartTime.text = it
            }

            tvEndTime -> DateUtils.selectTime(this,"选择开始时间"){
                tvEndTime.text = it
            }

            etProjectName -> {
                ProjectDialog(this,projectId).show{id,name ->
                    etProjectName.text = name
                    projectId = id
                }
            }

            cbAll -> {
                sign = -1
                cbAll.isChecked = true
                cbNormal.isChecked = false
                cbOverTime.isChecked = false
            }

            cbNormal -> {
                sign = 1
                cbAll.isChecked = false
                cbNormal.isChecked = true
                cbOverTime.isChecked = false
            }

            cbOverTime -> {
                sign = 0
                cbAll.isChecked = false
                cbNormal.isChecked = false
                cbOverTime.isChecked = true
            }

            btnPositive -> initData()
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        //page = 0
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page++
        initData()
    }
}